package com.qding.park.common.utils.validate;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

public class DataTimeFormatValid implements ConstraintValidator<DataTimeFormat, String> {

	private DataTimeFormat constraintAnnotation;

	@Override
	public void initialize(DataTimeFormat constraintAnnotation) {
		this.constraintAnnotation = constraintAnnotation;
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		
		if(StringUtils.isBlank(value)){
			//为空就不验证了
			return true;
		}
		String format = constraintAnnotation.format();
		SimpleDateFormat df = new SimpleDateFormat(format);
		Date d = null;
		df.setLenient(false);

		try {
			d = df.parse(value);
		} catch (Exception e) {
			d = new Date();
			System.out.println("你输入的日期不合法，请重新输入");
			return false;
		}
		return true;
	}

}
